package com.example.mymusicplayer;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SwitchCompat;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private ImageView iv_cover; //唱片图片
    private static SeekBar sb; // 进度条控件 数据更新跟线程有关需要static
    private static TextView tv_current,tv_total;
    private Button btn_play,bnt_pause,bnt_continue,bnt_exit;

    private ObjectAnimator animator; // 动画组件
    private MusicService.MusicControl musicControl; //音乐控制 服务中的Binder对象
    private Intent intent; //全局意图
    private MyserviceConn myserviceConn; //连接对象


    private boolean isUnbind = false; //服务是否被解绑

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    public void initView(){
        //获取控件
        iv_cover = findViewById(R.id.iv_cover);
        //绑定动画
        animator = ObjectAnimator.ofFloat(iv_cover,"rotation",0f,360.0f);
        animator.setDuration(10000); //旋转一周用时10S
        animator.setInterpolator(new LinearInterpolator()); //线性插值：迅速运动
        animator.setRepeatCount(-1); //动画无限循环

        intent = new Intent(this,MusicService.class); //打开服务
        myserviceConn = new MyserviceConn();
        //可以绑定服务了
        bindService(intent,myserviceConn,BIND_AUTO_CREATE);//绑定服务

        //进度条
        sb = findViewById(R.id.sb);
        // 拖拉事件
        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            // 进度条变化调用该方法
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if(progress == seekBar.getMax()) // 进度条到头
                    animator.pause(); //停止动画
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }
            // 滑动条停止滑动的事件处理
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int progress = seekBar.getProgress();//获取进度值
                musicControl.seekTo(progress); //改变播放器的进度值

            }
        });

        tv_current = findViewById(R.id.tv_current);
        tv_total = findViewById(R.id.tv_total);

        // 按钮
        btn_play = findViewById(R.id.bnt_play);
        bnt_pause = findViewById(R.id.bnt_pause);
        bnt_continue = findViewById(R.id.bnt_continue);
        bnt_exit = findViewById(R.id.bnt_exit);
        //为按钮添加点击事件
        btn_play.setOnClickListener(this);
        bnt_pause.setOnClickListener(this);
        bnt_continue.setOnClickListener(this);
        bnt_exit.setOnClickListener(this);

    }

    // 到主线程来更新界面进度
    //创建消息处理对象
    public static Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            //super.handleMessage(msg);
            Bundle bundle = msg.getData(); //接收子线程消息
            int duration = bundle.getInt("duration");
            int progress = bundle.getInt("progress");
            //设置滑动条的最大值 即通过这个跟音频文件时长挂钩
            sb.setMax(duration);
            sb.setProgress(progress); //设置当前进度

            //设置歌曲总时长
            tv_total.setText(time(duration));
            //设置歌曲当前时长
            tv_current.setText(time(progress));

        }
        //处理时间格式
        public String time(int data){
            //设置歌曲总时长
            int m = data / 1000 / 60;
            int sec = data / 1000 % 60;
            String strMin = "";
            String strsec = "";
            strMin =  m < 10 ?   "0" + m :  m + "";
            strsec = sec < 10 ?   "0" + sec :  sec + "";
            return strMin + ":"+strsec;
        }

    };
    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.bnt_play:
                //播放音乐
                musicControl.play();
                //开启动画
                animator.start();
                break;
            case R.id.bnt_pause:
                //暂停
                musicControl.pausePlay();
                animator.pause();
                break;
            case R.id.bnt_continue:
                // 继续
                musicControl.continuePlay();
                animator.start();
                break;
            case R.id.bnt_exit:
                //退出程序
                myUnbind();// 解绑服务
                finish(); //退出界面结束活动
                break;
        }
    }
    //解绑方法
    public void myUnbind (){
        if(!isUnbind){
            isUnbind = true;
            unbindService(myserviceConn);// 解绑服务
            stopService(intent); //停止服务
        }
    }
    // 用于实现连接服务的自定义类
    class MyserviceConn implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            musicControl = (MusicService.MusicControl)service; //获取连接服务的IBinder对象
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }
}